Rust Amplify Library: derive macros
Amplifying Rust language capabilities: multiple generic trait implementations, type wrappers, derive macros.
This is a part of Rust language amplification library providing required derive macros.
Minimum supported rust compiler version (MSRV): 1.60.0. Rust edition 2021.
Overview
Display derive
- Generate [
Display
] descriptions using other formatting trait: - Use existing function for displaying descriptions:
Formatting function must return [; union Int
String
] and take a singleself
argument (if you need formatting with streamed output, use one of existing formatting traits as shown in pt. 1). - Custom format string:
- Use of doc comments for descrition representation. In this case doc
comments may also contain formatting like in the case 3:
You can also mix in this mode with other fors of display tags on a specific options; in this case doc comments are ignoredextern crate amplify; ; assert_eq!; assert_eq!;
Example
Advanced use with enums:
Error derive
Error derive macro works to the full extend only when other derive macros
are used. With #[derive(Display)]
and [display(doc_comments)]
it uses
doc comments for generating error descriptions; with #[derive(From)]
it
may automatically implement transofrations from other error types.
From derive
Implements [From
] trait for the whole entity and/or its separate fields.
Works well with #[derive(Error)]
and, in many cases may require
[Default
] implementation (for details, pls see Examples below)
Examples
// Structure may contain no parameters
;
// When no explicit binding is given, structure must implement `Default`
;
Wrapper derive
Creates rust new type wrapping existing type. Can be used in sturctures
containing multiple named or unnamed fields; in this case the field you'd
like to wrap should be marked with #[wrap]
attribute; otherwise the first
field is assumed to be the wrapped one.
Use with multiple fileds requires that you do From
and Default
derive
on the main structure.
Supports automatic implementation of the following traits:
amplify::Wrapper
AsRef
AsMut
Borrow
BorrowMut
Deref
DerefMut
Complete usage of this derive macro is possible only with nightly rust
compiler with trivial_bounds
feature gate set for the crate and nightly
feature set. This will give you an automatic implementation for additional
traits, it they are implemented for the wrapped type:
Display
LowerHex
UpperHex
LowerExp
UpperExp
Octal
Index
IndexMut
Add
AddAssign
Sub
SubAssign
Mul
MulAssign
Div
DivAssign
Other traits, such as PartialEq
, Eq
, PartialOrd
, Ord
,
Hash
can be implemented using standard #[derive]
attribute in the
same manner as Default
, Debug
and From
Example
use PhantomData;
use Wrapper;
where
U: Sized + Clone;
let w = default;
assert_eq!;
Getters derive
Creates getter methods matching field names for all fields within a structure (including public and private fields). Getters return reference types.
Example
let one = default;
assert_eq!;
assert_eq!;
assert_eq!;
AsAny derive
Trait [amplify::AsAny
] allows simple conversion of any type into a generic
"thick" pointer &dyn Any
(see [::core::any::Any
]), that can be later
converted back to the original type with a graceful failing for all other
conversions. AsAny
derive macro allows to implement this trait for
arbitrary time without much hussle:
Example
# extern crate amplify_derive;
extern crate amplify;
use AsAny;
let mut point = Point ;
let point_ptr = point.as_any;
let mut circle = Circle ;
let circle_ptr = circle.as_any;
assert_eq!;
assert_eq!;
assert_eq!;
let p = point_ptr..unwrap;
assert_eq!